package ink.xiaobaibai.controller.buy;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ink.xiaobaibai.common.BuyPayBo;
import ink.xiaobaibai.common.MySecurity;
import ink.xiaobaibai.common.WxPayment;
import ink.xiaobaibai.entity.AdminUser;
import ink.xiaobaibai.entity.BuyOrder;
import ink.xiaobaibai.entity.LogisticsBuy;
import ink.xiaobaibai.multipleClick.ForbidMultipleClick;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.*;
import ink.xiaobaibai.serviceOxy.pay.IPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @description: 购买业务控制层
 * @author: 小白白
 * @create: 2021-06-13
 **/

@RestController
@RequestMapping("/buy/order")
@Api(tags = "购买业务控制层")
@Slf4j
public class BuyOrderController {

    @Autowired
    private IBuyOzoneDeviceService ozoneDeviceService;

    @Autowired
    private IBuyOxygenDeviceService oxygenDeviceService;

    @Autowired
    private IBuyOrderService buyOrderService;

    @Autowired
    private IAdminUserService adminUserService;

    @Autowired
    private IPayService payService;

    @Autowired
    private ILogisticsBuyService logisticsBuyService;

    /**
     * 查看自己的购买业务订单
     */
    @GetMapping("/list")
    @ApiOperation("查看自己的购买订单")
    public ResponseFormat getList(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        Integer userId = MySecurity.getUserId();
        QueryWrapper<BuyOrder> q1 = new QueryWrapper<>();
        q1.eq("client_user_id", userId);
        q1.orderByDesc("create_time");
        Page<BuyOrder> page = this.buyOrderService.page(new Page<>(pageNum, pageSize), q1);
        return ResponseFormat.success(page);
    }

    /**
     * 下单购买
     */
    @PostMapping("/pay")
    @ApiOperation("下单购买机器|由用户自己输入支付金额")
    @ForbidMultipleClick(prefixUserId = true, key = "#buyPayBo.adminUserId")
    public ResponseFormat payBuyDevice(@RequestBody @Validated BuyPayBo buyPayBo) {
        Integer userId = MySecurity.getUserId();
        BigDecimal amount = buyPayBo.getAmount();
        Integer adminUserId = buyPayBo.getAdminUserId();
        QueryWrapper<AdminUser> q1 = new QueryWrapper<>();
        q1.eq("id", adminUserId);
        if (this.adminUserService.count(q1) == 0) {
            return ResponseFormat.fail("当前业务员不存在");
        }
        //应该在支付回调中保存订单信息
        //返回下单信息
        this.buyOrderService.setBuyPayBoInfo(buyPayBo, userId);
        WxPayment wxPayment = this.payService.buyPay(amount, userId, adminUserId);
        return ResponseFormat.success(wxPayment);
    }

    /**
     * 购买订单item
     */
    @GetMapping("/item")
    @ApiOperation("购买订单详细 | 订单详细+机器型号+客户信息")
    public ResponseFormat item(@RequestParam("orderId") String orderId) {
        BuyOrder order = this.buyOrderService.getById(orderId);
        Integer logisticsId = order.getLogisticsBuyId();
        LogisticsBuy logisticsBuy = this.logisticsBuyService.getById(logisticsId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("order", order);
        jsonObject.put("logistics", logisticsBuy);
        return ResponseFormat.success(jsonObject);
    }

}
